package com.hfy.kandian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hfy.kandian.dao.command.UserInfoCommand;
import com.hfy.kandian.dao.representation.UserRepresentation;
import com.hfy.kandian.entity.User;
import com.hfy.kandian.mapper.UserMapper;
import com.hfy.kandian.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAN XIAOYANG
 * @since 2020-12-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public int loginByPhoneNum(String userPhone, String userPassword) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(User::getUserPhone,userPhone).eq(User::getUserPassword,userPassword);
        System.out.println("------->【【【【"+userPhone);
        User user=userMapper.selectOne(lambdaQueryWrapper);
        int res=0;
        if(user==null){
           res=0;
        }else{
            res=1;
        }
        return res;
    }

    @Override
    public int register(UserInfoCommand userInfoCommand) {
        User user = userInfoCommand.convertToUser();
        return userMapper.insert(user);
    }

    @Override
    public int modify(UserInfoCommand userInfoCommand) {
        User user = userInfoCommand.convertToUser();
        return userMapper.updateById(user);
    }

    @Override
    public UserRepresentation findById(int userId) {
        return userMapper.selectById(userId).convertToUserRepresentation();
    }

    @Override
    public int loginBySMSCode(String phoneNum) {
        //判断当前手机是否在数据库中
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(phoneNum),"user_phone",phoneNum);
        if(userMapper.selectOne(lambdaQueryWrapper)==null){
            return -1;
        }else{
            //TODO:短信发送接口
        }
        return 0;
    }
}
